package com.atguigu.bean.zqk.servlet;

import com.atguigu.bean.zqk.bean.User;
import com.atguigu.bean.zqk.service.UserService;
import com.atguigu.bean.zqk.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Date:2020/9/7
 * Author:ybc
 * Description:
 */
public class RegistServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码
        request.setCharacterEncoding("UTF-8");
        //获取请求参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String email = request.getParameter("email");
        //处理业务逻辑，即调用service
        UserService userService = new UserServiceImpl();
        //将从浏览器端收集的用户信息封装到User对象中
        User user = new User(null, username, password, email);
        boolean result = userService.regist(user);
        //判断注册的结果
        if(result){
            //表示注册成功，需要使用重定向跳转到注册成功页面
            response.sendRedirect(request.getContextPath() + "/pages/user/regist_success.html");
        }else{
            //表示注册失败，需要通过转发跳转到原页面即注册页面
            request.getRequestDispatcher("/pages/user/regist.html").forward(request, response);
        }
    }
}
